#include "solidobject.h"

SolidObject::SolidObject() :
    Point3D(), material(), texture(0), diffuse(.2),specular(.2), specularPower(50.0), reflective(.2), refractive(.2), refractiveN(1.0)
{

}

SolidObject::~SolidObject()
{
    if( texture && !(texture->isShared()) )
    {
        delete texture;
        texture = 0;
    }
}

SolidObject::SolidObject(const SolidObject &sObject) :
    Point3D(sObject), material(sObject.material), texture(0), diffuse(sObject.diffuse), specular(sObject.specular),
    specularPower(sObject.specularPower), reflective(sObject.reflective), refractive(sObject.refractive), refractiveN(sObject.refractiveN)
{

}

SolidObject& SolidObject::operator=(const SolidObject &sObject)
{
    setPosition(sObject.x,sObject.y,sObject.z);
    material      = sObject.material;

    diffuse       = sObject.diffuse;
    specular      = sObject.specular;
    specularPower = sObject.specularPower;
    reflective    = sObject.reflective;
    refractive    = sObject.refractive;
    refractiveN   = sObject.refractiveN;
    return (*this);
}

SolidObject::SolidObject(const Point3D &position, const Color &color) :
    Point3D(position), material(color), texture(0), diffuse(.2),specular(.2), specularPower(50), reflective(.2), refractive(.2), refractiveN(1.0)
{

}

SolidObject::SolidObject(const double &x, const double &y, const double &z, const double &red, const double &green, const double &blue) :
    Point3D(x,y,z), material(red,green,blue), texture(0), diffuse(.2),specular(.2), specularPower(specularPower), reflective(.2), refractive(.2)
{

}


bool SolidObject::isTextured() const
{
    return texture;
}

inline void  SolidObject::setColor(const Color &color)
{
    material = color;
}

inline void  SolidObject::setColor(const double &red, const double &green, const double &blue)
{
    material = Color(red,green,blue);
}

inline void SolidObject::setMaterialComponents(const double &_diffuse, const double &_specular, const double &_specularPower, const double &_reflective, const double &_refractive, const double &_refractiveN)
{
    /*double denom(_ambiant+_diffuse+_specular+_reflective+_refractive);
    denom      = ((denom>1.0)?denom:1.0);

    diffuse    = _diffuse/denom;
    specular   = _specular/denom;
    reflective = _reflective/denom;
    refractive = _refractive/denom;*/

    diffuse       = _diffuse;
    specular      = _specular;
    specularPower = _specularPower;
    reflective    = _reflective;
    refractive    = _refractive;
    refractiveN   = _refractiveN;

}

inline Color SolidObject::getColor() const
{
    return material;
}

inline Color SolidObject::getDiffuseColor() const
{
    return material*diffuse;
}

inline double SolidObject::getSpecularCoef() const
{
    return specular;
}

inline double SolidObject::getSpecularPower() const
{
    return specularPower;
}

inline double SolidObject::getReflectiveCoef() const
{
    return reflective;
}

inline double SolidObject::getRefractiveCoef() const
{
    return refractive;
}

inline double SolidObject::getRefractiveNumber() const
{
    return refractiveN;
}

void SolidObject::attachTexture(Texture *_texture)
{
    texture = _texture;
}

inline bool SolidObject::isReflective() const
{
    return (reflective>0.0);
}

inline bool SolidObject::isRefractive() const
{
    return (refractive>0.0);
}
